package com.twu.biblioteca.processor;

import com.twu.biblioteca.controller.BookController;
import com.twu.biblioteca.controller.LibraryController;
import com.twu.biblioteca.controller.UserController;
import com.twu.biblioteca.io.InputReader;
import com.twu.biblioteca.io.InputValidator;
import com.twu.biblioteca.model.Library;
import com.twu.biblioteca.model.User;
import com.twu.biblioteca.ui.ItemView;
import com.twu.biblioteca.util.Messages;

/**
 * Created by madhu on 2/5/14.
 */
public class ReturnBookMenuProcessor implements MenuProcessor {
    @Override
    public void processLibraryMenu(LibraryController libraryController) {
        if(!User.isLoggedIn()) UserController.promptForLogin(libraryController.getLibraryView());
        else returnBook(getBookNameToReturn(), libraryController.getLibrary());
    }

    public void returnBook(String bookNameToReturn, Library library) {
        new BookController().returnBook(bookNameToReturn, library.getBooks());
    }

    public String getBookNameToReturn() {
        ItemView itemView = new ItemView();
        itemView.displayMessage(Messages.bookNamePromptMessage);
        String input;
        while(true) {
            input = new InputReader().getUserInput();
            if(InputValidator.isEmpty(input)) itemView.displayMessage(Messages.emptyInputMessage);
            else break;
        }
        return input;
    }

}
